/**
 * Created by wqz806687102 on 2016/1/19.
 */
'use strict';

//Õý³£
angular.module('QC').controller('QualityRecordController', ['$scope', '$state', 'PublicModule', 'PublicEnum', 'PublicFunction', 'PublicService', '$location', 'Authentication',
    function ($scope, $state, PublicModule, PublicEnum, PublicFunction, PublicService, $location, Authentication) {

        $scope.BarcodeMaster;
        $scope.QCStatusEnum;
        PublicService.getExtendEnum('QCStatus', function(extendEnum){
            $scope.QCStatusEnum = extendEnum;
        });
        $scope.QCTypeEnum;
        PublicService.getExtendEnum('QCType', function(extendEnum){
            $scope.QCTypeEnum = extendEnum;
        });

        $scope.BadTypeEnum;
        PublicService.getExtendEnum('BadType', function(extendEnum){
            $scope.BadTypeEnum = extendEnum;
        });
        $scope.BadPositionEnum;
        PublicService.getExtendEnum('BadPosition', function(extendEnum){
            $scope.BadPositionEnum = extendEnum;
        });

        $scope.btnSave = function(){
            for (var i = 0; i < $scope.Module.QCRecord.Records.length; i++) {
                if($scope.Module.QCRecord.Records[i].BizRecord === $scope.Module.BizRecord.FocusedRecord._id){
                    $scope.Module.QCRecord.Records.splice(i, 1);
                    i--;
                }
            };
            $scope.Module.QCRecord.Records = $scope.Module.QCRecord.Records.concat($scope.Module._QCRecord.Records);
            $scope.Module.QCRecord.save(function(){
                $scope.Module.BizRecord.save(function(){
                    $state.reload();
                });
                
            });
        };
        $scope.btnCancel = function(){
            $state.reload();
        };
        //查询
        $scope.btnQuery=function(){
            if ($scope.BarcodeMaster==undefined||$scope.BarcodeMaster.trim()=="") {
                PublicFunction.alert("芯片ID不能为空！", "查询错误");
            };
            $scope.Module.BizRecord.find({BarcodeMaster:$scope.BarcodeMaster},null,function(records){
                    $scope.Module.BizRecord.clearRecords();
                    $scope.Module.QCRecord.clearRecords();
                    $scope.Module._QCRecord.clearRecords();
                    $scope.Module.BizRecord.Records=records;
                    $scope.Module.BizRecord.RecordCount=records.length;
                    $scope.Module.BizRecord.firstPage();
                });
        };
        //验证指标值
        $scope.cellValueChange=function(columnName, newValue, oldValue){
            if (columnName === "NormValue"&&$scope.Module._QCRecord.FocusedRecord.QCNorm.Type===0) {
                var regInt=/^[-+]?\d*$/;
                var flInt=regInt.test(this.Module._QCRecord.FocusedRecord.NormValue.trim());
                var regDeci=/^[-\+]?\d+(\.\d+)?$/;
                var flDeci=regDeci.test(this.Module._QCRecord.FocusedRecord.NormValue.trim());
                if(!flInt&&!flDeci){
                    this.Module._QCRecord.FocusedRecord.NormValue=0;

                    PublicFunction.alert("当前指标类型的指标值只能为数字和小数！", "输入错误"); 
                    $scope.$digest();                  
                }
            };
            
        };
        var oldBRFocusRecord=null;
        $scope.rowChange = function(){
            if(oldBRFocusRecord != null){
                for(var i=0; i<$scope.Module.QCRecord.Records.length; i++){
                    if($scope.Module.QCRecord.Records[i].BizRecord === oldBRFocusRecord._id){
                        $scope.Module.QCRecord.Records.splice(i, 1);
                        i--;
                    }
                }
                $scope.Module.QCRecord.Records = $scope.Module.QCRecord.Records.concat($scope.Module._QCRecord.Records);
            }
            oldBRFocusRecord=$scope.Module.BizRecord.FocusedRecord;
            $scope.Module._QCRecord.clearRecords();
            if ($scope.Module.BizRecord.FocusedRecord.IsLoadQCRecord) {
                var _QCRecordRecords = [];
                for(var i=0; i<$scope.Module.QCRecord.Records.length; i++){
                    if ($scope.Module.QCRecord.Records[i].BizRecord==$scope.Module.BizRecord.FocusedRecord._id) {
                        _QCRecordRecords.push($scope.Module.QCRecord.Records[i]);
                    };
                }
                $scope.Module._QCRecord.Records = _QCRecordRecords;
                $scope.Module._QCRecord.RecordCount = _QCRecordRecords.length;
                $scope.Module._QCRecord.firstPage();
            } else{
                $scope.Module.QCRecord.find({BizRecord:$scope.Module.BizRecord.FocusedRecord._id},null,function(records){
                    $scope.Module.BizRecord.FocusedRecord.IsLoadQCRecord=true;
                    $scope.Module._QCRecord.Records=records;
                    $scope.Module._QCRecord.RecordCount=records.length;
                    $scope.Module._QCRecord.firstPage();
                    $scope.Module.QCRecord.Records = $scope.Module.QCRecord.Records.concat(records);
                });
            };
        };

        PublicModule.initModule($scope, 'BizRecord');
        $scope.Module.BizRecord.LoadMode = PublicEnum.LoadMode.CacheAndNoPaging;
        $scope.Module.BizRecord.PageSize = 15;
        $scope.Module.BizRecord.QueryParameter.Populate = [{path: 'Operation', select: 'Name'},{path: 'QCUser', select: 'Code Name'}];
        //$scope.Module.BizRecord.load();

        PublicModule.initModule($scope, 'QCRecord');
        $scope.Module.QCRecord.LoadMode = PublicEnum.LoadMode.CacheAndNoPaging;
        $scope.Module.QCRecord.QueryParameter.Populate = {path: 'QCNorm', select: 'Name Type'};
        $scope.Module.QCRecord.PageSize = 15;

        PublicModule.initModule($scope, '_QCRecord');
        $scope.Module._QCRecord.LoadMode = PublicEnum.LoadMode.CacheAndNoPaging;
        $scope.Module._QCRecord.PageSize = 15;
    }
]);

//Â¼ÈëÆ·ÖÊ¼ÇÂ¼
//angular.module('QC').controller('QualityRecordController',
//    ['$scope', '$stateParams', 'PublicModule', 'PublicFunction', '$location', 'Authentication',
//        function ($scope, $stateParams, PublicModule, PublicFunction, $location, Authentication) {
//            $scope.BadPositionEnum;
//            PublicService.getExtendEnum('BadPosition', function(extendEnum){
//                $scope.BadPositionEnum = extendEnum;
//            });
//            $scope.BadTypeEnum;
//            PublicService.getExtendEnum('BadType', function(extendEnum){
//                $scope.BadTypeEnum = extendEnum;
//            });
//            $scope.btnSave = function () {
//                $scope.Module.QCRecord.save();
//            };
//
//            $scope.btnAdd = function () {
//                $scope.Module.QCRecord.add();
//            };
//            $scope.btnDelete = function () {
//                $scope.Module.QCRecord.delete();
//            };
//
//            //³õÊ¼»¯
//            PublicModule.initModule($scope,'QCRecord');
//
//            $scope.Module.QCRecord.QueryParameter.Populate = [{path: 'BizRecord', select: '_id'},{path: 'QCNorm', select: 'Code Name'}];
//
//            if($stateParams.id){
//                $scope.Module.QCRecord.findById($stateParams.id,function(record){
//                    $scope.Module.QCRecord.FocusedRecord=record;
//                })
//            }
//            else {
//                $scope.btnAdd();
//            };
//            $scope.Menu='Ê×Ò³';
//        }
//    ]);

//Â¼ÈëÁ÷Ë®
//angular.module('QC').controller('QualityRecordController',
//    ['$scope', '$stateParams', 'PublicModule', 'PublicFunction', '$location', 'Authentication',
//        function ($scope, $stateParams, PublicModule, PublicFunction, $location, Authentication) {
//            $scope.BusinessStatusEnum;
//            PublicService.getExtendEnum('BusinessStatus', function(extendEnum){
//                $scope.BusinessStatusEnum = extendEnum;
//            });
//            $scope.QCTypeEnum;
//            PublicService.getExtendEnum('QCType', function(extendEnum){
//                $scope.QCTypeEnum = extendEnum;
//            });
//            $scope.QCStatusEnum;
//            PublicService.getExtendEnum('QCStatus', function(extendEnum){
//                $scope.QCStatusEnum = extendEnum;
//            });
//            $scope.btnSave = function () {
//                $scope.Module.BizRecord.save();
//            };
//
//            $scope.btnAdd = function () {
//                $scope.Module.BizRecord.add();
//            };
//            $scope.btnDelete = function () {
//                $scope.Module.BizRecord.delete();
//            };
//
//            //³õÊ¼»¯
//            PublicModule.initModule($scope,'BizRecord');
//
//            $scope.Module.BizRecord.QueryParameter.Populate = [{path: 'BarcodeMaster', select: 'Barcode'},{path: 'Terminal', select: 'Code Name'},{path: 'Operation', select: 'Name'},{path: 'Work', select: 'Code Name'},{path: 'WorkLocation', select: 'Code Name'},{path: 'QCUser', select: 'Code Name'}];
//
//            if($stateParams.id){
//                $scope.Module.BizRecord.findById($stateParams.id,function(record){
//                    $scope.Module.BizRecord.FocusedRecord=record;
//                })
//            }
//            else {
//                $scope.btnAdd();
//            };
//            $scope.Menu='Ê×Ò³';
//        }
//    ]);


